﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ExportToRTR.CApExportToRTRLocations
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.ExportToRTR.Resources;
using EngineeringInternalExtension;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.IO;
using System.Threading;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.ExportToRTR{

public class CApExportToRTRLocations
{
  private const string LOCATIONS_FILE_NAME_SUFFIX = ".txt";
  private const string CONFIG_NAMES = "ConfigNames";
  private const char LOCATIONS_DELIMITER = ',';
  internal TxRobot m_robot;
  private readonly string m_fileName;
  internal List<ITxRoboticLocationOperation> m_locations = new List<ITxRoboticLocationOperation>();
  private List<TxJoint> m_externalJoints;

  public CApExportToRTRLocations(TxRobot robot, List<TxJoint> externalJoints, string filePath)
  {
    this.m_robot = robot;
    this.m_externalJoints = externalJoints;
    this.m_fileName = this.GetFileName(filePath);
  }

  internal void Write(TxReportWriterEx logWriter, CApExportToRTRLocationsInfo locationsInfo)
  {
    CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
    Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-US");
    Dictionary<string, int> dictionary = new Dictionary<string, int>();
    using (StreamWriter streamWriter = new StreamWriter(this.m_fileName))
    {
      streamWriter.NewLine = "\n";
      string[] jointNames = this.GetJointNames();
      streamWriter.WriteLine(this.FormatLine("ConfigNames", (object[]) jointNames));
      foreach (ITxRoboticLocationOperation location in this.m_locations)
      {
        string name = ((ITxObject) location).Name;
        if (location.RobotConfigurationData == null)
        {
          ++locationsInfo.NonConfiguredCounter;
          logWriter.WriteLine(string.Format(ExportToRTRStringTable.LOCATION_NOT_CONFIGURED_WARNING_LOG, (object) name, (object) ((ITxObject) location.ParentRoboticOperation).Name, (object) ((TxBaseGeometryCreationObject) this.m_robot).Name));
        }
        try
        {
          using (TxPoseData poseAtLocation = this.m_robot.GetPoseAtLocation(location))
          {
            ArrayList jointValues = poseAtLocation.JointValues;
            if (this.m_externalJoints.Count > 0)
            {
              List<TxRobotExternalAxisData> externalAxisDataList = location.RobotExternalAxesData != null ? new List<TxRobotExternalAxisData>((IEnumerable<TxRobotExternalAxisData>) location.RobotExternalAxesData) : (List<TxRobotExternalAxisData>) null;
              foreach (TxJoint externalJoint in this.m_externalJoints)
              {
                TxJoint joint = externalJoint;
                double num1 = joint.CurrentValue;
                if (externalAxisDataList != null && externalAxisDataList.Exists((Predicate<TxRobotExternalAxisData>) (x => TxJoint.op_Equality(x.Joint, (ITxObject) joint))))
                  num1 = externalAxisDataList.Find((Predicate<TxRobotExternalAxisData>) (x => TxJoint.op_Equality(x.Joint, (ITxObject) joint))).JointValue;
                double num2 = joint.Type == 2 ? num1 * (1.0 / 1000.0) : num1;
                jointValues.Add((object) num2);
              }
            }
            streamWriter.WriteLine(this.FormatLine(name, jointValues.ToArray()));
            dictionary[name] = dictionary.ContainsKey(name) ? dictionary[name] + 1 : 1;
          }
        }
        catch
        {
          ++locationsInfo.InvalidCounter;
          logWriter.WriteLine(string.Format(ExportToRTRStringTable.LOCATION_NOT_VALID_ERROR_LOG, (object) ((TxBaseGeometryCreationObject) this.m_robot).Name, (object) name, (object) ((ITxObject) location.ParentRoboticOperation).Name));
        }
      }
    }
    foreach (KeyValuePair<string, int> keyValuePair in dictionary)
    {
      if (keyValuePair.Value > 1)
      {
        ++locationsInfo.DuplicateCounter;
        logWriter.WriteLine(string.Format(ExportToRTRStringTable.LOCATION_NOT_UNIQUE_WARNING_LOG, (object) keyValuePair.Key, (object) ((TxBaseGeometryCreationObject) this.m_robot).Name, (object) keyValuePair.Value));
      }
    }
    Thread.CurrentThread.CurrentCulture = currentCulture;
  }

  internal void AddLocation(ITxRoboticLocationOperation location) => this.m_locations.Add(location);

  private string GetFileName(string filePath)
  {
    string validFileName = CApExportToRTRUtils.GetValidFileName(((TxBaseGeometryCreationObject) this.m_robot).Name);
    return Path.Combine(filePath, string.Format(ExportToRTRStringTable.LOCATIONS_FILE_NAME, (object) validFileName, (object) ".txt"));
  }

  private string FormatLine(string title, object[] arr)
  {
    return $"{title},{string.Join(','.ToString(), arr)}";
  }

  private string[] GetJointNames()
  {
    TxObjectList drivingJoints = this.m_robot.DrivingJoints;
    string[] jointNames = new string[((Collection<ITxObject>) drivingJoints).Count + this.m_externalJoints.Count];
    int index1;
    for (index1 = 0; index1 < ((Collection<ITxObject>) drivingJoints).Count; ++index1)
      jointNames[index1] = $"{((TxBaseGeometryCreationObject) this.m_robot).Name}/{((Collection<ITxObject>) drivingJoints)[index1].Name}";
    int index2 = 0;
    while (index2 < this.m_externalJoints.Count)
    {
      jointNames[index1] = $"{((TxBaseGeometryCreationObject) this.m_robot).Name}/{((ITxObject) this.m_externalJoints[index2].Device).Name}/{this.m_externalJoints[index2].Name}";
      ++index2;
      ++index1;
    }
    return jointNames;
  }
}
